/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

import entities.ProjColumn;
import java.util.Collection;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.persistence.EntityManager;
import entities.Project;
import converter.ProjColumnsConverter;
import converter.ProjColumnConverter;
import javax.persistence.PersistenceContext;
import javax.ejb.Stateless;

/**
 *
 * @author Alex
 */

@Path("/projColumns/")
@Stateless
public class ProjColumnsResource {

    @javax.ejb.EJB
    private ProjColumnResource projColumnResource;

    @Context
    protected UriInfo uriInfo;

    @PersistenceContext(unitName = "uTaskPU")
    protected EntityManager em;
  
    /** Creates a new instance of ProjColumnsResource */
    public ProjColumnsResource() {
        
    }

    /**
     * Get method for retrieving a collection of ProjColumn instance in XML format.
     *
     * @return an instance of ProjColumnsConverter
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public ProjColumnsConverter get(@QueryParam("start") @DefaultValue("0") int start,
                                    @QueryParam("max") @DefaultValue("10") int max,
                                    @QueryParam("expandLevel") @DefaultValue("1")int expandLevel,
                                    @QueryParam("query") @DefaultValue("SELECT e FROM ProjColumn e") String query) {
        return new ProjColumnsConverter(getEntities(start, max, query), uriInfo.getAbsolutePath(), expandLevel);
    }

    /**
     * Post method for creating an instance of ProjColumn using XML as the input format.
     *
     * @param data an ProjColumnConverter entity that is deserialized from an XML stream
     * @return an instance of ProjColumnConverter
     */
    @POST
    @Consumes({"application/xml", "application/json"})
    public Response post(ProjColumnConverter data) {
        ProjColumn entity = data.resolveEntity(em);
        createEntity(data.resolveEntity(em));
        return Response.created(uriInfo.getAbsolutePath().resolve(entity.getId() + "/")).build();
    }

    /**
     * Returns a dynamic instance of ProjColumnResource used for entity navigation.
     *
     * @return an instance of ProjColumnResource
     */
    @Path("{id}/")
    public ProjColumnResource getProjColumnResource(@PathParam("id") Long id) {
        projColumnResource.setId(id);
        projColumnResource.setEm(em);
        return projColumnResource;
    }

    @GET
    @Path("projId={projId}/")
    @Produces({"application/xml", "application/json"})
    /*
     * Find project columns by specified project id
     */
    public ProjColumnsConverter getProjColumnByProject(@PathParam("projId") Long projId) {
        Collection<ProjColumn> columns = 
                em.createNamedQuery("ProjColumn.findByColumnId").setParameter("projId", projId).getResultList();
        ProjColumnsConverter projConv = new ProjColumnsConverter(columns, uriInfo.getAbsolutePath(), 1);

        return projConv;
    }

    /**
     * Returns all the entities associated with this resource.
     *
     * @return a collection of ProjColumn instances
     */
    protected Collection<ProjColumn> getEntities(int start, int max, String query) {
        return em.createQuery(query).setFirstResult(start).setMaxResults(max).getResultList();
    }

    /**
     * Persist the given entity.
     *
     * @param entity the entity to persist
     */
    protected void createEntity(ProjColumn entity) {
        long maxId = 0;
        try {
            maxId = (Long)em.createQuery("SELECT MAX(n.id) FROM ProjColumn n").getSingleResult();
        } catch (Exception ex) {
            //no records in db, defaulted to 0
        }
        entity.setId(maxId);
        em.persist(entity);
    }
}
